import jieba
import re

import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
from wordcloud import WordCloud

if __name__ == '__main__':
    with open('a.txt', encoding='utf-8') as f:
        mytext = f.read()
        mytext = re.sub(r'[\n？“”：……！，。·]', '', mytext)
        mytext = " ".join(jieba.cut(mytext))
    mask = np.array(Image.open('img.png'))
    stopwords = {'的', '和', '是', '都', '而', '就',
                 '都', '而', '或', '如', '以', '我',
                 '你', '他', '她', '它', '们', '它们',
                 '我们', '你们', '他们', '她们', '的',
                 '了', '着', '有', '无', '不', '么', '在',
                 '也', '让', '吧', '这', '啊', '有些', '不过',
                 '还', '那', '对', '道', '得'}

    wc = WordCloud(
        font_path='msyhl.ttc',
        mask=mask,
        background_color="white",
        stopwords=stopwords,
        repeat=True
    ).generate(mytext)

    wc.to_file('cloud.jpg')

    plt.axis('off')
    plt.imshow(wc, interpolation='bilinear')